package com.mulanweb.handler;

import com.jfinal.handler.Handler;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Ret;
import com.mulanweb.db.common.Constant;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by zhf on 2016/4/11.
 */
public class LoginVerifyHandler extends Handler {

    @Override
    public void handle(String target, HttpServletRequest req, HttpServletResponse resp, boolean[] isHandled) {
        if (target.matches(".*/mulanweb/seller/.*") || target.matches(".*/seller-view/.*")) {
            judgeSellerLogin(target,req, resp,isHandled);
        } else if (target.matches(".*/mulanweb/admin/.*") || target.matches(".*/admin-view/.*")) {
            judgeAdminLogin(target,req, resp,isHandled);
        }/* else if (target.matches(".*//*mulanweb/wechat/.*")) {
            judgeUserLogin(req, resp);
            isHandled[0] = true;
        }*/ else {
            super.next.handle(target, req, resp, isHandled);
        }

        //String auth = req.getHeader("Authorization");
        /*if (StringUtils.isBlank(auth) || isNotExists(auth)) {
            notLoginHandler(resp);
            isHandled[0] = true;
        } else {
            super.next.handle(target, req, resp, isHandled);
        }*/
    }

    /**
     * 判断编码的令牌是否有效
     *
     * @param encryptedToken 编码过的令牌
     * @return 如果编码过的令牌存在，返回true，否则返回false
     */
    /*private boolean isNotExists(String encryptedToken) {
        String sql = "SELECT * FROM " + TableMapping.me().getTable(MAdminLogon.class).getName() + " WHERE EncryptedToken = ?";
        List<MAdminLogon> records = MAdminLogon.dao.findByCache(BasicCacheNames.AdminLogon.FIND_BY_ENCRYPTED_TOKEN, encryptedToken, sql, encryptedToken);
        return CollectionUtils.isEmpty(records);
    }*/

    /**
     * 判断商家是否登录，未登录返回到商家登录页，登录则通过
     *
     * @param resp 响应信息
     */
    private void judgeSellerLogin(String target, HttpServletRequest req, HttpServletResponse resp,boolean[] isHandled) {
        String sellerLoginTag = (String) req.getSession().getAttribute(Constant.XinXiLan.SELLER_LOGIN_TAG);
        if (!(sellerLoginTag != null && "yes".equals(sellerLoginTag))) {
            try {
                resp.sendRedirect(Constant.XinXiLan.SELLER_LOGIN_URL);
                isHandled[0] = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
            super.next.handle(target,req,resp,isHandled);
        }
    }

    /**
     * 判断管理员是否登录，未登录返回到管理员登录页，登录则通过
     *
     * @param resp 管理员响应信息
     */
    private void judgeAdminLogin(String target, HttpServletRequest req, HttpServletResponse resp,boolean[] isHandled) {
        String adminLoginTag =  (String) req.getSession().getAttribute(Constant.XinXiLan.ADMIN_LOGIN_TAG);
        if (!(adminLoginTag != null && "yes".equals(adminLoginTag))) {
            try {
                resp.sendRedirect(Constant.XinXiLan.ADMIN_LOGIN_URL);
            } catch (IOException e) {
                e.printStackTrace();
            }
            isHandled[0] = true;
        }else{
            super.next.handle(target,req,resp,isHandled);
        }
    }

    /**
     * 判断用户是否授权，未授权提示用户使用微信公众号打开页面
     *
     * @param resp 管理员响应信息
     */
    private void judgeUserLogin(String target, HttpServletRequest req, HttpServletResponse resp,boolean[] isHandled) {
        String adminLoginTag = req.getSession().getAttribute("openId").toString();
        if (!(adminLoginTag != null)) {
            try {
                resp.sendRedirect(Constant.XinXiLan.SELLER_LOGIN_URL);
            } catch (IOException e) {
                e.printStackTrace();
            }
            isHandled[0] = true;
        }else{
            super.next.handle(target,req,resp,isHandled);
        }
    }

}



